﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 事件订阅系统演示
{ 
    class Program
    {
        static void Main(string[] args)
        {
            bank ban = new bank();
            Console.Write("输入取款金额：");
            string amount = Console.ReadLine();
            Console.WriteLine( "取款成功，请取走银行卡！");
            Usereventarg user = new Usereventarg("zzz@163.com","13570190000",amount);
            subscribesystem sub = new subscribesystem(ban,user);
            Console.ReadKey();

        }
    }
    public class Usereventarg:EventArgs
    {
        public string email;
        public string mobile;
        public string amount;

        public Usereventarg(string email, string mobile, string amount)
        {
            this.email = email;
            this.mobile = mobile;
            this.amount = amount;        
        }
    } 
    public class bank
    {
        public delegate void processstraneventhandler(object sender, Usereventarg e);//声明事件
        public event processstraneventhandler pro;//定义事件
        //事件处理
        protected virtual void Onpro(Usereventarg e)
        {
            if (e != null)
            {
                pro(this, e);
            }
        }
        //触发事件
        public void process(Usereventarg e)
        {
            Onpro(e);
        }
    }
    //事件观察者
    public class emaile
    {
        public static void sendemail(object sender,Usereventarg e)
        {
            Console.WriteLine($"向{e.email}发送信息：在{System.DateTime.Now}取款{e.amount}");
        }
    }
    public class mobile
    {
        public static void sendesms(object sender, Usereventarg e)
        {
            Console.WriteLine($"向{e.mobile}发送信息：在{System.DateTime.Now}取款{e.amount}");
        }
    }
    public class subscribesystem
    {
        public subscribesystem() { }
        public subscribesystem(bank b,Usereventarg e)
        {
            b.pro += emaile.sendemail;
            b.pro += mobile.sendesms;
            b.process(e);
        }

    }
}
